package com.hxd.mall.module.product

import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hxd.mall.R
import com.shizhefei.view.largeimage.LargeImageView
import org.jetbrains.anko.displayMetrics

class ProductImageListAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.adapter_product_detail_item_layout) {
    override fun convert(helper: BaseViewHolder?, item: String?) {
        val imageView = helper?.getView<LargeImageView>(R.id.iv_detail)
        imageView?.setPadding(0,0,0,10)
        Glide.with(imageView!!.context).load(item ?: "").apply(RequestOptions().placeholder(R.drawable.banner_default_icon)).into(object : SimpleTarget<Drawable>() {
            override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                val bitmap = resource.toBitmap()
                val params = helper.itemView.layoutParams

                val height = (bitmap.height * imageView.context.displayMetrics.widthPixels) / bitmap.width
                params.height = height
                helper.itemView.layoutParams = params
                imageView.setImage(resource)
            }
        })
    }
}